package com.bangdream.michelia.view.fragment.main.task;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.entity.CommentBean;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.view.activity.questionnaire.QuestionnaireActivity;
import com.bangdream.michelia.view.adapter.TaskCurriculumAdapter;
import com.bangdream.michelia.view.adapter.TaskQuestListAdapter;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/6/18 17:13
 * @email 909732184@qq.com
 * 任务-问卷
 */
public class TaskQuestList extends LazyLoadFragment<CoursesContract.ICoursesView,CoursesPresenter<CoursesContract.ICoursesView>>
        implements CoursesContract.ICoursesView{
    private View view;
    //刷新加载控件
    private XRefreshView refreshView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private TaskQuestListAdapter adapter;
    private int page = 1;
    private List<CoursesBean> dataList;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.recycler_view_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setListener();
    }

    private void setListener() {
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                loadData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                loadData();
            }
        });

        adapter.setOnItemClickListener(new TaskQuestListAdapter.OnItemClickListener() {
            @Override
            public void onExaminationItemClick(int position) {
                startActivity(new Intent(getActivity(), QuestionnaireActivity.class));
            }
        });
    }

    @Override
    public CoursesPresenter createPresenter() {
        return new CoursesPresenter();
    }

    @Override
    public void loadData() {
        Map params = new HashMap();
        mPresenter.getCoursesListData(
                page, PublicResources.PageSize, CoursesContract.CoursesType.Ordinary,params,null);
    }

    private void initView() {
        refreshView = view.findViewById(R.id.refreshView);
        refreshView.setPullRefreshEnable(true);
        refreshView.setPullLoadEnable(false);
        recyclerView = view.findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new TaskQuestListAdapter(getActivity());
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void setCoursestList(boolean isSuccess, int page, CoursesContract.CoursesType type, List<CoursesBean> data, String stDescribe) {
        if(isSuccess&&data!=null){
            if(page==1) {
                dataList = data;
                adapter.setData(dataList);
                adapter.notifyDataSetChanged();
                refreshView.stopRefresh();
                //显示数据大于20条才允许上拉加载更多
                if(dataList.size()>PublicResources.PageSize){
                    refreshView.setPullLoadEnable(true);
                }
            }else {
                int tempIndex = adapter.getData().size();
                for (int i = 0;i<data.size();i++) {
                    adapter.getData().add(data.get(i));
                }
                adapter.notifyItemChanged(tempIndex,adapter.getData().size());
                refreshView.stopLoadMore();
            }
        }else {
            //如果是加载新数据时失败了 则page-1
            if (page>1){
                refreshView.stopLoadMore();
                this.page--;
            }else {
                //还是还原为1防止出现负数
                this.page = 1;
                refreshView.stopRefresh();
            }
        }
    }

    @Override
    public void setCoursesDetails(boolean isSuccess, CoursesBean data, String stDescribe) {

    }

    @Override
    public void setCoursesTypeList(boolean isSuccess, List<CoursesTypeBean> data, String stDescribe) {

    }

    @Override
    public void setCommentList(boolean isSuccess, int page, List<CommentBean> data, String stDescribe) {

    }

    @Override
    public void actionComment(boolean isSuccess, String stDescribe) {

    }

}
